package copy.type;

import java.math.BigDecimal;

public class BigDecimalType {
    public static String bigDecimal(Object value1,Object value2,String sym,int scale,int round){
        //默认取舍方式
        if (round == -1){
            round = BigDecimal.ROUND_UNNECESSARY;
        }
        BigDecimal bigDecimalOne = new BigDecimal(value1 + "");
        BigDecimal bigDecimalTwo = new BigDecimal(value2 + "");
        BigDecimal resultBigDecimal = null;
        if (sym.equals("++")){
            resultBigDecimal = bigDecimalOne.add(bigDecimalTwo);
        }else if (sym.equals("**")){
            resultBigDecimal = bigDecimalOne.multiply(bigDecimalTwo);
        }else if (sym.equals("//")){
            //除法精度: 默认小数点后6位(跟C语言保持一致)
            if (scale == -1){
                scale = 6;
            }
            return bigDecimalOne.divide(bigDecimalTwo,scale,round).toString();
        }else if (sym.equals("--")){
            resultBigDecimal = bigDecimalOne.subtract(bigDecimalTwo);
        }
        //默认精度: 运算结果的本身
        if (scale == -1){
            scale = resultBigDecimal.scale();
        }
        return resultBigDecimal.setScale(scale,round).toString();
    }
}
